package com.hzcc.tagmanagesystemapi.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzcc.tagmanagesystemapi.common.ResultResponse;
import com.hzcc.tagmanagesystemapi.common.enums.ApiFileEnums;
import com.hzcc.tagmanagesystemapi.module.dao.ApiFileDao;
import com.hzcc.tagmanagesystemapi.module.dao.ApiNameDao;
import com.hzcc.tagmanagesystemapi.module.entity.po.ApiFile;
import com.hzcc.tagmanagesystemapi.module.entity.po.ApiName;
import com.hzcc.tagmanagesystemapi.module.service.ApiFileService;
import com.hzcc.tagmanagesystemapi.utils.DateTimeUtil;
import com.hzcc.tagmanagesystemapi.utils.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

/**
 * @Author 早睡蛋
 * @Date 2022/5/22 13:56:15
 * @Desc:文档service业务层
 */

@Service
public class ApiFileServiceImpl extends ServiceImpl<ApiFileDao, ApiFile> implements ApiFileService {

    @Autowired
    private ApiFileDao apiFileDao;
    @Autowired
    private ApiNameDao apiNameDao;

    /**
     * 添加
     * @param apiFile
     * @return
     */
    @Override
    public ResultResponse addApiFile(ApiFile apiFile) {
        if (apiFile.getApiName()==""||apiFile.getApiName()==null){
            return ApiFileEnums.NAMEisNULL.getResultResponse();
        }else{
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.select("id").eq("api_name",apiFile.getApiName());
            ApiFile apiFile1 = apiFileDao.selectOne(queryWrapper);
            if (apiFile1!=null){
                return ApiFileEnums.NAMEisEXISTED.getResultResponse();
            }else {
                String id = IDUtil.createByUUID();
                apiFile.setId(id);
                ApiName apiName = new ApiName();
                apiName.setId(id);
                apiName.setApiName(apiFile.getApiName());
                int flag = apiFileDao.insert(apiFile);
                int flag2 = apiNameDao.insert(apiName);
                if (flag>0&&flag2>0){
                    return ApiFileEnums.isADD.getResultResponse();
                }else{
                    return ApiFileEnums.noADD.getResultResponse();
                }
            }
        }

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResultResponse deleteApiFile(String id) {
        int flag = apiFileDao.deleteById(id);
        int flag2 = apiNameDao.deleteById(id);
        if (flag>0&&flag2>0){
            return ApiFileEnums.isDELETE.getResultResponse();
        }else{
            return ApiFileEnums.noDELETE.getResultResponse();
        }
    }

    /**
     * 更新
     * @param apiFile
     * @return
     */
    @Override
    public ResultResponse updateApiFile(ApiFile apiFile) throws ParseException {
        UpdateWrapper<ApiFile> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",apiFile.getId());
        ApiFile apiFile1 = new ApiFile();
        apiFile1.setApiContent(apiFile.getApiContent());
        apiFile1.setUpdateTime(DateTimeUtil.getCurrentDateTime());
        int flag = apiFileDao.update(apiFile1, updateWrapper);
        if (flag>0){
            return ApiFileEnums.isUPDATE.getResultResponse();
        }else {
            return ApiFileEnums.noUPDATE.getResultResponse();
        }
    }

    /**
     * 所有接口名称列表
     * @return
     */
    @Override
    public List<ApiName> allApiName() {
        List<ApiName> apiNameList = apiNameDao.selectList(null);
        return apiNameList;
    }

    /**
     * 通过id查询接口文档
     * @param id
     * @return
     */
    @Override
    public ApiFile oneApiFile(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("*").eq("id",id);
        ApiFile apiFile = apiFileDao.selectOne(queryWrapper);
        return apiFile;
    }
}
